package com.wzy.controller;

import com.wzy.entity.Kt;
import com.wzy.entity.KtQk;
import com.wzy.service.ExportService;
import com.wzy.service.KtQkService;
import com.wzy.service.KtSevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("ktqk")
public class KtQkController {
    private static final Logger log = LoggerFactory.getLogger(KtQkController.class);

    private KtSevice ktSevice;

    private KtQkService qkService;
    private ExportService exportService;
   @Autowired
    public KtQkController(KtSevice ktSevice, KtQkService qkService, ExportService exportService) {
        this.ktSevice = ktSevice;
        this.qkService = qkService;
        this.exportService = exportService;
    }

    @RequestMapping("choose")
    public String update(KtQk qk,Kt kt) throws IOException {

        log.debug("选择之后课题情况信息: id:{},名称:{}, 学生:{}, 状态:{} ,日期:{},级别:{}", qk.getId(),qk.getKname(),qk.getYname(),qk.getState(),qk.getTime(),qk.getRank());
        qkService.save(qk);
        return "redirect:/ktqk/lists";//更新成功,跳转到研究生列表
    }

    @RequestMapping("detail")
    public String detail(Integer kid, Model model) {
        log.debug("当前查询课题id: {}", kid);
        //1.根据id查询一个
        Kt kt = ktSevice.findById(kid);
        model.addAttribute("kt", kt);
        return "chooseKt";//跳转到选择页面
    }

    @RequestMapping("detail2")
    public String detail2(Integer id, Model model) {
        log.debug("当前查询课题id: {}", id);
        //1.根据id查询一个
        KtQk qk = qkService.findById(id);
        model.addAttribute("qk", qk);
        return "updateQk";//跳转到更新页面
    }

    @RequestMapping("lists")
    public String lists(Model model){
        log.debug("查询所有课题情况");
        List<KtQk> qkList = qkService.lists();
        model.addAttribute("qkList",qkList);
        return "ktqklist";
    }
    @RequestMapping("delete")
    public String delete(Integer id){
        log.debug("删除的情况id: {}",id);
        //1.删除数据
        qkService.delete(id);
        return "redirect:/ktqk/lists";//跳转到员工列表
    }
    @RequestMapping("update")
    public String update(KtQk qk) throws IOException {

        log.debug("更新之后课题信息: id:{},名称:{}, 学生姓名:{}, 项目状态:{} ,日期:{},等级:{}", qk.getId(),qk.getKname(),qk.getYname(),qk.getState(),qk.getTime(),qk.getRank());
        qkService.update(qk);
        return "redirect:/ktqk/lists";//更新成功,跳转到研究生列表
    }

    @RequestMapping("/exportFile")
    public void exportFile(HttpServletResponse response) {
       exportService.exportFile(response);
    }

}

